package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.config.Result;
import com.example.demo.dao.MessageServer;
import com.example.demo.entity.Message;
import com.example.demo.mapper.MessageMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lu
 * @Date: 2024-04-15-20:36
 * @Description:
 */
@RestController
@RequestMapping("/selectPage")
@Api(tags = "mybatisplus分页查询")
public class MessageController {
    @Autowired
    MessageServer messageServer;

    @Autowired
    MessageMapper messageMapper;


    @PostMapping("/pageList/{current}/{size}")
    public Result<IPage<Message>> getPage(@PathVariable("current") int current, @PathVariable("size") int size, @RequestBody Message param){

        return Result.success(messageServer.selectPage(current,size,param));
    }

    @PostMapping("/list")
    public Result<List<Message>> getlist (@RequestBody  Message message){
        List<Message> list = messageServer.list(new LambdaQueryWrapper<Message>()
                .like(Message::getPhone, message.getPhone())
                .select(Message::getUserName,Message::getPhone)
                .orderByDesc(Message::getEmail));
        return Result.success(list);
    }

    @PostMapping("/selectlist")
    public Result<List<Message>> selectList(@RequestBody Message message){
        List<Message> messages = messageMapper.selectList(new LambdaQueryWrapper<Message>().eq(Message::getPassWord, message.getPassWord()));
        return Result.success(messages);
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody Message message) {
        if (message.getId() != null) {
            Message messageServerById = messageServer.getById(message.getId());
            messageServerById.setEmail(message.getEmail() + 1);
            messageServer.update(new LambdaUpdateWrapper<Message>()
                    .eq(Message::getId, message.getId())
                    .set(Message::getPassWord, message.getPassWord()));
        }else {
            return Result.fail("false");
        }
        return Result.success("true");

    }


}
